package com.huangpuweb.task.controller;

import com.huangpuweb.task.Constants;
import com.huangpuweb.task.model.QzYYGFData;
import com.huangpuweb.task.model.SysHpScenicState;
import com.huangpuweb.task.model.SysRefinedCrowdSituation;
import com.huangpuweb.task.service.SyncQzYYGFDataService;
import com.huangpuweb.task.service.SyncSysDataService;
import com.huangpuweb.task.util.DateUtil;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.UUID;


/**
 * 同步客流监控平台数据-定时任务
 *
 * 同步豫园外圈、田子坊、
 * 新天地、外滩客流数据
 */
@Controller
public class SyncDataFromYYGFJob {

    private static Logger logger = Logger.getLogger(SyncDataFromYYGFJob.class);



    private SyncQzYYGFDataService syncQzYYGFDataService;
    @Autowired
    public void setSyncQzYYGFDataService(SyncQzYYGFDataService syncQzYYGFDataService) {
        this.syncQzYYGFDataService = syncQzYYGFDataService;
    }

    private SyncSysDataService syncSysDataService;
    @Autowired
    public void setSyncSysDataService(SyncSysDataService syncSysDataService) {
        this.syncSysDataService = syncSysDataService;
    }


    /**
     * 定时同步豫园股份数据
     * @throws Exception
     */
    public void syncDataFromYYGFTask() throws Exception {



        LogFactory.getLog(this.getClass()).info("--------start-------syncDataFromYYGFTask----------"+new Date());
        /**
         * 同步豫园股份数据到Sys_refined_crowdSituation表
         */
        syncDataFromYYGF();
        /**
         * 清洗数据到_state表中
         */
        saveScenicStateData();
        LogFactory.getLog(this.getClass()).info("--------end-------syncDataFromYYGFTask----------"+new Date());





    }

    private void saveScenicStateData() {

        String[] spotCodeArr = Constants.SPOTCODELIST.split(",");

        //获取当前日期的0点和23点
        String todayZero = DateUtil.getTodayZero();
        String today23   =DateUtil.getToday23(23,59,59);


        //获取截至目前人流量峰值和时间,景区名称、景区编号
        SysRefinedCrowdSituation  maxSysrefinedcrowd=syncSysDataService.getMaxNumberBySysRefinedCrowdSituation("SHHPQ_YUYUAN_NEIQUAN",todayZero,today23);

        //获取最新的实施人流量和今日总人流量
        SysRefinedCrowdSituation  realSysrefinedcrowd=syncSysDataService.getRealNumberBySysRefinedCrowdSituation("SHHPQ_YUYUAN_NEIQUAN");

        SysHpScenicState sysHpScenicState=new SysHpScenicState();

        if(maxSysrefinedcrowd != null && realSysrefinedcrowd!=null){
            //设置景区名称
            sysHpScenicState.setSpotName(maxSysrefinedcrowd.getSpotName());
            //设置景区编码
            sysHpScenicState.setSpotCode("SHHPQ_YUYUAN_NEIQUAN");
            //设置景区今日峰值
            sysHpScenicState.setMaxNumber(maxSysrefinedcrowd.getRealtimeNumber());
            //设置景区峰值产生时间
            sysHpScenicState.setMaxTime(maxSysrefinedcrowd.getCreateTime());
            //设置景区实时人流量
            int realtimeNumber = Integer.valueOf(realSysrefinedcrowd.getRealtimeNumber());
            sysHpScenicState.setRealTimeNumber(realtimeNumber);
            //设置景区总人流量
            sysHpScenicState.setTotalNumber(Integer.valueOf(realSysrefinedcrowd.getTotalNumber()));
            //设置景区面积 SPOTCODELIST
            String area="2";
            sysHpScenicState.setSpotArea(area);
            //设置人群密度
            DecimalFormat df = new DecimalFormat("0.00");
            double _area = Double.valueOf(area) * 10000;
            String density = df.format(realtimeNumber/_area);
            sysHpScenicState.setPeopleDensity(density);
            //设置景区监控点
            //TODO
            //获取景区摄像头数量
            String spotmonitornumber="0";


            int monitorNumber=0;
            if (null != spotmonitornumber) {
                monitorNumber=Integer.valueOf(spotmonitornumber);
            }
            sysHpScenicState.setMonitorNumber(monitorNumber);
            //设置风险等级
            sysHpScenicState.setRiskLevel(realtimeNumber);
            sysHpScenicState.setRecordTime(DateUtil.getDateTime());
            //更新数据 hp_scenic_state
            syncSysDataService.updateHpScenicState(sysHpScenicState);
        }


    }


    /**
     * 开始同步豫园股份数据，数据包含豫园商城实时人流量数据
     */
    private void syncDataFromYYGF() {


        String startTime = DateUtil.getTodayZero();

        String endTime=DateUtil.getToday23(23,59,59);

        String yygfspotcode ="P00001";


        //获取实时人流、数据创建时间、
        QzYYGFData qzYYGFData=syncQzYYGFDataService.getQzYYGFData(startTime,endTime,yygfspotcode);

        SysRefinedCrowdSituation sysrefinedcrowd=null;
        if (null!=qzYYGFData){
            sysrefinedcrowd=new SysRefinedCrowdSituation();
            sysrefinedcrowd.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            //设置数据创建时间
            sysrefinedcrowd.setCreateTime(qzYYGFData.getCountDate());
            //设置数据更新时间
            sysrefinedcrowd.setUpdateTime(DateUtil.getDateTime());
            //设置景区名称
            sysrefinedcrowd.setSpotName("豫园（内圈）");
            //设置景区编码
            sysrefinedcrowd.setSpotCode("SHHPQ_YUYUAN_NEIQUAN");
            //设置实时人流
            sysrefinedcrowd.setRealtimeNumber(qzYYGFData.getRealTimeNum());
            //设置累计宗人流量
            sysrefinedcrowd.setTotalNumber(qzYYGFData.getSum());
            //设置数据来源
            sysrefinedcrowd.setDataSources("豫园股份");
            //设置对接方式
            sysrefinedcrowd.setRemarks("数据库对接");
            //插入数据
            syncSysDataService.insertSysRefinedCrowdSituation(sysrefinedcrowd);
            //插入数据到备份表中
            syncSysDataService.insertSysBackupCrowdSituation(sysrefinedcrowd);

        }
    }


}
